package com.stone.framework.workflow.definition.handler.assignment;

import java.util.Collection;
import java.util.List;

import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.process.handler.AssignmentHandler;
import com.bstek.uflo.process.node.TaskNode;

public abstract class AbstractAssignmenthandler implements AssignmentHandler {

    @Override
    public Collection<String> handle(TaskNode taskNode, ProcessInstance processInstance, Context context) {
        String role = getAssigneeRole(taskNode, processInstance, context);
        String promoter = getPromoter(taskNode, processInstance, context);
        List<String> assignees = getAssignees(promoter, role);
        List<String> finalAssignees = handleAssignees(assignees);
        sendMessage(taskNode, processInstance, context, finalAssignees);
        return finalAssignees;
    }

    protected abstract String getAssigneeRole(TaskNode taskNode, ProcessInstance processInstance, Context context);

    protected abstract String getPromoter(TaskNode taskNode, ProcessInstance processInstance, Context context);

    protected abstract List<String> getAssignees(String promoter, String role);

    protected abstract List<String> handleAssignees(List<String> assignees);

    // TODO async?
    protected abstract void sendMessage(TaskNode taskNode, ProcessInstance processInstance, Context context,
        List<String> assignees);

}
